<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Calculation Protocol Reference</title>
    <link rel="stylesheet" type="text/css" href="../css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="../css/highlight.css" />
    <meta charset='utf-8'>
    <script src="../js/jquery.min.js" defer></script>
    <script src="../js/jazzy.js" defer></script>
    
    <script src="../js/lunr.min.js" defer></script>
    <script src="../js/typeahead.jquery.js" defer></script>
    <script src="../js/jazzy.search.js" defer></script>
  </head>
  <body>
    <a name="//apple_ref/swift/Protocol/Calculation" class="dashAnchor"></a>
    <a title="Calculation Protocol Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="../index.html"> Docs</a> (27% documented)</p>
        <p class="header-right"><a href="https://github.com/jjkaufman/SwiftProcessing"><img src="../img/gh.png"/>View on GitHub</a></p>
        <p class="header-right">
          <form role="search" action="../search.json">
            <input type="text" placeholder="Search documentation" data-typeahead>
          </form>
        </p>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="../index.html"> Reference</a>
        <img id="carat" src="../img/carat.png" />
        Calculation Protocol Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="../Drawing.html">Drawing</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Protocols/Shapes.html">Shapes</a>
              </li>
              <li class="nav-group-task">
                <a href="../Protocols/Attributes.html">Attributes</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../Math.html">Math</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Protocols/Calculation.html">Calculation</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            <h1>Calculation</h1>
              <div class="declaration">
                <div class="language">
                  
                  <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">protocol</span> <span class="kt">Calculation</span></code></pre>

                </div>
              </div>
            <p>Useful functions for common math calculations</p>

          </section>
          <section class="section task-group-section">
            <div class="task-group">
              <ul>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:15SwiftProcessing11CalculationP9constrainy12CoreGraphics7CGFloatVAG_A2GtF"></a>
                    <a name="//apple_ref/swift/Method/constrain(_:_:_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:15SwiftProcessing11CalculationP9constrainy12CoreGraphics7CGFloatVAG_A2GtF">constrain(_:<wbr>_:<wbr>_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Constrains a value between a minimum and maximum value.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">constrain</span><span class="p">(</span><span class="n">_</span> <span class="nv">n</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">low</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">high</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">CGFloat</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>n</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>number to constrain</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>low</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>minimum limit</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>high</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>maximum limit</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:15SwiftProcessing11CalculationP8distancey12CoreGraphics7CGFloatVAG_A3GtF"></a>
                    <a name="//apple_ref/swift/Method/distance(_:_:_:_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:15SwiftProcessing11CalculationP8distancey12CoreGraphics7CGFloatVAG_A3GtF">distance(_:<wbr>_:<wbr>_:<wbr>_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Calculates the distance between two points</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">distance</span><span class="p">(</span><span class="n">_</span> <span class="nv">x1</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">y1</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">x2</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">y2</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">CGFloat</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>x1</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>x-coordinate of the first point</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>y1</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>y-coordinate of the first point</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>x2</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>x-coordinate of the second point</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>y2</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>y-coordinate of the second point</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:15SwiftProcessing11CalculationP4lerpy12CoreGraphics7CGFloatVAG_A2GtF"></a>
                    <a name="//apple_ref/swift/Method/lerp(_:_:_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:15SwiftProcessing11CalculationP4lerpy12CoreGraphics7CGFloatVAG_A2GtF">lerp(_:<wbr>_:<wbr>_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Calculates a number between two numbers at a specific increment</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">lerp</span><span class="p">(</span><span class="n">_</span> <span class="nv">start</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">stop</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">amt</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">CGFloat</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>start</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>first value</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>stop</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>second value</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>amt</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>amount to interpolate between the two values</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:15SwiftProcessing11CalculationP3magy12CoreGraphics7CGFloatVAG_AGtF"></a>
                    <a name="//apple_ref/swift/Method/mag(_:_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:15SwiftProcessing11CalculationP3magy12CoreGraphics7CGFloatVAG_AGtF">mag(_:<wbr>_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Calculates the magnitude (or length) of a vector</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">mag</span><span class="p">(</span><span class="n">_</span> <span class="nv">a</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">b</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">CGFloat</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>a</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>first value</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>b</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>second value</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:15SwiftProcessing11CalculationP3mapy12CoreGraphics7CGFloatVAG_A4GSbtF"></a>
                    <a name="//apple_ref/swift/Method/map(_:_:_:_:_:_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:15SwiftProcessing11CalculationP3mapy12CoreGraphics7CGFloatVAG_A4GSbtF">map(_:<wbr>_:<wbr>_:<wbr>_:<wbr>_:<wbr>_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Re-maps a number from one range to another.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">map</span><span class="p">(</span><span class="n">_</span> <span class="nv">value</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">start1</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">stop1</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">start2</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">stop2</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">withinBounds</span><span class="p">:</span> <span class="kt">Bool</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">CGFloat</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>value</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>the incoming value to be converted</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>start1</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>lower bound of the value&rsquo;s current range</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>stop1</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>upper bound of the value&rsquo;s current range</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>start2</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>lower bound of the value&rsquo;s target range</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>stop2</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>upper bound of the value&rsquo;s target range</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>withinBounds</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>constrain the value to the newly mapped range (Optional)</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:15SwiftProcessing11CalculationP3maxy12CoreGraphics7CGFloatVSayAGGF"></a>
                    <a name="//apple_ref/swift/Method/max(_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:15SwiftProcessing11CalculationP3maxy12CoreGraphics7CGFloatVSayAGGF">max(_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Determines the largest value in a sequence of numbers, and then returns that value</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">max</span><span class="p">(</span><span class="n">_</span> <span class="nv">array</span><span class="p">:</span> <span class="p">[</span><span class="kt">CGFloat</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kt">CGFloat</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>array</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>Numbers to compare</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:15SwiftProcessing11CalculationP3miny12CoreGraphics7CGFloatVSayAGGF"></a>
                    <a name="//apple_ref/swift/Method/min(_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:15SwiftProcessing11CalculationP3miny12CoreGraphics7CGFloatVSayAGGF">min(_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Determines the smallest value in a sequence of numbers, and then returns that value</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">min</span><span class="p">(</span><span class="n">_</span> <span class="nv">array</span><span class="p">:</span> <span class="p">[</span><span class="kt">CGFloat</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kt">CGFloat</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>array</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>Numbers to compare</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:15SwiftProcessing11CalculationP4normy12CoreGraphics7CGFloatVAG_A2GtF"></a>
                    <a name="//apple_ref/swift/Method/norm(_:_:_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:15SwiftProcessing11CalculationP4normy12CoreGraphics7CGFloatVAG_A2GtF">norm(_:<wbr>_:<wbr>_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Normalizes a number from another range into a value between 0 and 1.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">norm</span><span class="p">(</span><span class="n">_</span> <span class="nv">num</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">start</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="n">_</span> <span class="nv">stop</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">CGFloat</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>num</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>incoming value to be normalized</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>start</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>lower bound of the value&rsquo;s current range</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>stop</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>upper bound of the value&rsquo;s current range</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:15SwiftProcessing11CalculationP2sqy12CoreGraphics7CGFloatVAGF"></a>
                    <a name="//apple_ref/swift/Method/sq(_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:15SwiftProcessing11CalculationP2sqy12CoreGraphics7CGFloatVAGF">sq(_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Squares a number (multiplies a number by itself).</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">sq</span><span class="p">(</span><span class="n">_</span> <span class="nv">num</span><span class="p">:</span> <span class="kt">CGFloat</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">CGFloat</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>num</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>number to square</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>squared number</p>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
          </section>
        </section>
        <section id="footer">
          <p>&copy; 2020 <a class="link" href="https://swiftprocessing.org/" target="_blank" rel="external">Swift Processing</a>. All rights reserved. (Last updated: 2020-10-19)</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.13.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</div>
</html>
